from __future__ import absolute_import, division, print_function, unicode_literals
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
AUTOTUNE = tf.data.experimental.AUTOTUNE
AUTOTUNE = tf.data.experimental.AUTOTUNE
AUTOTUNE = tf.data.experimental.AUTOTUNE
import IPython.display as display
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os
import glob
import pathlib
tf.__version__
from google.colab import drive
drive.mount('/content/drive')
# split to valid and training
data_dir="/content/drive/My Drive/Infa/IWiUM/transformed_train_zoom500_dim130x100/data_train/"
test_dir="/content/drive/My Drive/Infa/IWiUM/transformed_test_zoom500_dim130x100/data_test/"
data_dir = pathlib.Path(data_dir)
test_dir = pathlib.Path(test_dir)
image_count = len(list(data_dir.glob('*/*.png')))
image_count
CLASS_NAMES = np.array([item.name for item in data_dir.glob('*') if item.name != "LICENSE.txt"])
CLASS_NAMES
image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
BATCH_SIZE = 32
IMG_HEIGHT = 130
IMG_WIDTH = 100
STEPS_PER_EPOCH = np.ceil(image_count/BATCH_SIZE)
train_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = True,
fill_mode = "nearest",
zoom_range = 0.3,
width_shift_range = 0.3,
height_shift_range=0.3,
rotation_range=30,
validation_split=0.2
)
train_data_gen = train_datagen .flow_from_directory(directory=str(data_dir),
batch_size=BATCH_SIZE,
# shuffle=True,
target_size=(IMG_WIDTH, IMG_HEIGHT),
classes = list(CLASS_NAMES))
valid_data_gen = train_datagen .flow_from_directory(directory=str(data_dir),
batch_size=BATCH_SIZE,
# shuffle=True,
target_size=(IMG_WIDTH, IMG_HEIGHT),
classes = list(CLASS_NAMES))
def show_batch(image_batch, label_batch):
plt.figure(figsize=(10,10))
for n in range(25):
ax = plt.subplot(5,5,n+1)
plt.imshow(image_batch[n])
plt.title(CLASS_NAMES[label_batch[n]==1][0].title())
plt.axis('off')
image_batch, label_batch = next(train_data_gen)
show_batch(image_batch, label_batch)
valid_image_batch, valid_label_batch = next(valid_data_gen)
show_batch(valid_image_batch, valid_label_batch)
from __future__ import absolute_import, division, print_function, unicode_literals
import os
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
keras = tf.keras
IMG_SHAPE = (IMG_WIDTH, IMG_HEIGHT, 3)
IMG_SHAPE
base_model = tf.keras.applications.VGG19(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
#base_model.trainable = False
for layer in base_model.layers[:5]:
layer.trainable = False
base_model.summary()
image_batch.shape
feature_batch = base_model(image_batch)
print(feature_batch.shape)
from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D
from keras import optimizers
layer1 = keras.layers.Dense(1024, activation="relu")
layer1_batch = layer1(feature_batch)
print(layer1_batch.shape)
layer2 = keras.layers.Dropout(rate=0.5)
layer2_batch = layer2(layer1_batch)
print(layer2_batch.shape)
layer3 = keras.layers.Dense(1024, activation="relu")
layer3_batch = layer3(layer2_batch)
print(layer3_batch.shape)
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(layer3_batch)
print(feature_batch_average.shape)
prediction_layer = keras.layers.Dense(2, activation="softmax")
prediction_batch = prediction_layer(feature_batch_average)
print(prediction_batch.shape)
model = tf.keras.Sequential([
base_model,
layer1,
layer2,
layer3,
global_average_layer,
prediction_layer
])
base_learning_rate = 0.0001
model.compile(optimizer=tf.keras.optimizers.SGD(lr=base_learning_rate, momentum=0.9),
loss='categorical_crossentropy',
metrics=['accuracy'])
model.summary()
len(model.trainable_variables)
initial_epochs = 50
steps_per_epoch = BATCH_SIZE
validation_steps = 20
loss0,accuracy0 = model.evaluate(valid_data_gen, steps = validation_steps)
print("initial loss: {:.2f}".format(loss0))
print("initial accuracy: {:.2f}".format(accuracy0))
history = model.fit(train_data_gen,
epochs=initial_epochs,
validation_data=valid_data_gen)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
history
loss = history.history['loss']
val_loss = history.history['val_loss']
initial = []
for i in range(len(val_acc)):
initial.append(accuracy0)
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.plot(initial, label="Initial Accuracy")
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()),1])
plt.title('Training and Validation Accuracy')
plt.xlabel('epoch')
plt.show()
path_to_save = '/content/drive/My Drive/Infa/IWiUM/models/model1'
model.save_weights(path_to_save)
# model.load_weights(path_to_save)
test_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = False,
)
test_data_gen = test_datagen.flow_from_directory(directory=str(test_dir),
batch_size=400,
shuffle=False,
target_size=(IMG_WIDTH,IMG_HEIGHT),
classes = list(CLASS_NAMES))
test_data = test_data_gen[0]
v_predictions = model.predict(test_data[0])
v_predictions_cl = v_predictions.tolist()
v_predictions_cl = list(map(lambda x: (x.index(max(x)), max(x)), v_predictions_cl))
classes = []
for cl in test_data[1]:
classes.append(np.argmax(cl))
i = 0
plt.figure(figsize=(10,10))
n=0
all = 0
nok = 0
print('TRUE : PREDICTED')
for (cl, val) in v_predictions_cl:
all += 1
if cl != classes[i]:
nok += 1
# print(str(classes[i]) + ' : ' + str(cl))
plt.imshow(test_data[0][i])
ax = plt.subplot(5,5,n+1)
plt.imshow(test_data[0][i])
plt.title(CLASS_NAMES[classes[i]] + ' : ' + CLASS_NAMES[cl])
plt.axis('off')
n+=1
# print(cl)
# print(CLASS_NAMES[cl])
# plt.imshow(test_data[0][i])
i += 1
print("Accuracy is: {}".format(float((1-nok/all)*100)))
for cl in test_data[1]:
classes.append(np.argmax(cl))
i = 0
plt.figure(figsize=(32,62))
n=0
print('TRUE : PREDICTED')
for (cl, val) in v_predictions_cl:
plt.imshow(test_data[0][i])
ax = plt.subplot(32,8,n+1)
plt.imshow(test_data[0][i])
plt.title(CLASS_NAMES[classes[i]] + ' : ' + CLASS_NAMES[cl])
plt.axis('off')
n+=1
i += 1